package cn.rulian.base.main.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rulian.base.auth.bo.Resource;
import cn.rulian.base.auth.bo.ResourceType;
import cn.rulian.base.auth.bo.Role;
import cn.rulian.base.auth.bo.User;
import cn.rulian.base.auth.dao.ResourceDao;
import cn.rulian.base.auth.dao.UserDao;
import cn.rulian.base.common.util.xx;
import cn.rulian.base.common.vo.TreeVo;

/**
 * 用户管理Service
 */
@Service
@Transactional(readOnly = true)
public class MainService 
{

	@Autowired
	private UserDao userDao;

	@Autowired
	private ResourceDao resourceDao;
	
		
	/**
	 * 根据用户名，查询该用户可以访问的菜单
	 */
	public List<TreeVo> queryResourceByUserName(String userName,String systemNo)
	{
		//查询所有菜单
		List<Resource> allResourceList = resourceDao.findByResourceType_codeOrderByOrderNum(ResourceType.MENU_CODE);  //所有菜单
	
		//构造可访问资源ID-Map，用于权限过滤
		Map<Long,Long> canMap = new HashMap<>(); //可访问资源ID-Map
		User user =userDao.findFirstByUsername(userName); //用户
		List<Role> roleList = user.getRoleList(); //拥有的角色
		if(!xx.isEmpty(roleList))
		{
			for(Role role:roleList)
			{
				List<Resource> resourceList = role.getResourceList();
				if(!xx.isEmpty(resourceList))
				{
					for(Resource re:resourceList)
					{
						canMap.put(re.getId(), re.getId());
					}
				}
			}
		}
		
		//构造可以访问的菜单列表
		//String systemNo = pro.getSystemNo(); //系统编号，0 是公共编码
		if(systemNo==null)
		{
			systemNo="0";
		}
		List<Resource> canList = new ArrayList<>(); 
		for(Resource re:allResourceList)
		{
			if(canMap.get(re.getId()) != null)
			{
				if(systemNo.equals(re.getSystemNo()) || "0".equals(re.getSystemNo()))
				{//本系统菜单 或 公共菜单
					canList.add(re);
				}
			}
		}
		
		if(User.DEV.equals(userName))
		{//开发人员，看到所有菜单
			canList = allResourceList;
		}
		
		//对象转换： Resource --> TreeVo
		List<TreeVo> canVoList = new ArrayList<>();
		for(Resource bo:canList)
		{
			TreeVo vo = new TreeVo();
			vo.setId(bo.getId()); //标识
			vo.setText(bo.getName()); //显示文本
			vo.setIconCls(bo.getIconCls()); //图标
			vo.setState(bo.getMenuState()); //状态：打开、关闭
			if(bo.getFather() != null)
			{
				vo.set_parentId(bo.getFather().getId()); //父ID（中间转换用）
			}
			
			//其他属性
			Map<String,Object> map =new HashMap<>();
			
			String url = bo.getUrl(); //其他属性：URL
			if(!xx.isEmpty(url))
			{
				 map.put("url", url);
			}
			
			map.put("openWin", bo.getOpenWin()); //其他属性：openWin
			
			vo.setAttributes(map); 
			
			canVoList.add(vo);
		}
		
		//构造子结构
		for(TreeVo dto:canVoList)
		{
			for(TreeVo son:canVoList)
			{
				if(son.get_parentId() != null && son.get_parentId().longValue() == dto.getId().longValue())
				{ 
					List<TreeVo> children = dto.getChildren();
					if(children==null)
					{
						children = new ArrayList<>();
					}
					children.add(son);
					dto.setChildren(children);
				}
			}
		}
		
		//只保留顶层元素
		List<TreeVo> showList = new ArrayList<>(); //显示列表
		for(TreeVo dto:canVoList)
		{
			if(dto.get_parentId() == null)
			{
				showList.add(dto);
			}
		}
		
		return showList;
	}
	

}
